package com.kzd.gtgame.main.mine

import android.os.Bundle
import android.text.Editable
import androidx.core.widget.addTextChangedListener
import com.blankj.utilcode.util.LogUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.UserAddress
import com.kzd.gtgame.jetpack.vm.AddressViewModel
import com.kzd.gtgame.main.KEY_ADDRESS_INFO
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_address_edit.*

class AddressEditFragment : OwnerViewModeFragment<AddressViewModel>() {

    companion object {
        fun newInstance(userAddress: UserAddress? = null): AddressEditFragment {
            val bundle = Bundle().apply {
                putSerializable(KEY_ADDRESS_INFO, userAddress)
            }
            return AddressEditFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mUserAddress: UserAddress by lazy {
        (arguments?.getSerializable(KEY_ADDRESS_INFO) as? UserAddress) ?: UserAddress()
    }

    override fun viewModel(): AddressViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_address_edit

    override fun initView() {


        mUserAddress.consignee?.let {
            etAddressName.text = Editable.Factory.getInstance().newEditable(it)
        }
        mUserAddress.mobile?.let {
            etAddressPhone.text = Editable.Factory.getInstance().newEditable(it)
        }
        mUserAddress.uaddress?.let {
            etAddressDetails.text = Editable.Factory.getInstance().newEditable(it)
        }
        cbAddressIsDefault.isChecked = mUserAddress.moren == "1"

        tvAddressKeep.isEnabled = !mUserAddress.consignee.isNullOrEmpty()
                && !mUserAddress.mobile.isNullOrEmpty()
                && !mUserAddress.uaddress.isNullOrEmpty()

        etAddressName.addTextChangedListener {
            tvAddressKeep.isEnabled = !it.isNullOrEmpty()
                    && !etAddressPhone.text.isNullOrEmpty()
                    && !etAddressDetails.text.isNullOrEmpty()
            mUserAddress.apply { consignee = it?.toString() }
        }
        etAddressPhone.addTextChangedListener {
            tvAddressKeep.isEnabled = !it.isNullOrEmpty()
                    && !etAddressName.text.isNullOrEmpty()
                    && !etAddressDetails.text.isNullOrEmpty()
            mUserAddress.apply { mobile = it?.toString() }
        }
        etAddressDetails.addTextChangedListener {
            tvAddressKeep.isEnabled = !it.isNullOrEmpty()
                    && !etAddressName.text.isNullOrEmpty()
                    && !etAddressPhone.text.isNullOrEmpty()
            mUserAddress.apply { uaddress = it?.toString() }
        }

        cbAddressIsDefault.setOnCheckedChangeListener { _, isChecked ->
            mUserAddress.apply {
                moren = if (isChecked) "1" else "0"
            }
        }

        tvAddressKeep.setOnClickAntiShakeListener {
            val id = mUserAddress.id
            val consignee = mUserAddress.consignee
                ?: return@setOnClickAntiShakeListener
            val mobile = mUserAddress.mobile
                ?: return@setOnClickAntiShakeListener
            val uaddress = mUserAddress.uaddress
                ?: return@setOnClickAntiShakeListener
            val moren = mUserAddress.moren
            viewModel.userAddressKeep(id, consignee, mobile, uaddress, moren)
        }
    }
}